home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / main / robot.c < prev    next >
C/C++ Source or Header  |  1998-03-03  |  8KB  |  326 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: /usr/CVS/descent/main/robot.c,v $
  15.  * $Revision: 1.1.1.1 $
  16.  * $Author: nobody $
  17.  * $Date: 1998/03/03 15:12:30 $
  18.  * 
  19.  * Code for handling robots
  20.  * 
  21.  * $Log: robot.c,v $
  22.  * Revision 1.1.1.1  1998/03/03 15:12:30  nobody
  23.  * reimport after crash from backup
  24.  *
  25.  * Revision 1.1.1.1  1998/02/13  20:21:03  hfrieden
  26.  * Initial Import
  27.  *
  28.  * Revision 2.1  1995/03/07  16:52:02  john
  29.  * Fixed robots not moving without edtiro bug.
  30.  * 
  31.  * Revision 2.0  1995/02/27  11:31:11  john
  32.  * New version 2.0, which has no anonymous unions, builds with
  33.  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
  34.  * 
  35.  * Revision 1.19  1995/02/22  13:58:09  allender
  36.  * remove anonymous unions from object structure
  37.  * 
  38.  * Revision 1.18  1995/01/27  11:17:06  rob
  39.  * Avoid problems with illegal gun num.
  40.  * 
  41.  * Revision 1.17  1994/11/19  15:15:02  mike
  42.  * remove unused code and data
  43.  * 
  44.  * Revision 1.16  1994/11/05  16:41:31  adam
  45.  * upped MAX_ROBOT_JOINTS
  46.  * 
  47.  * Revision 1.15  1994/09/26  15:29:29  matt
  48.  * Allow morphing objects to fire
  49.  * 
  50.  * Revision 1.14  1994/06/20  14:31:02  matt
  51.  * Don't include joint zero in animation data
  52.  * 
  53.  * Revision 1.13  1994/06/10  14:39:58  matt
  54.  * Increased limit of robot joints
  55.  * 
  56.  * Revision 1.12  1994/06/10  10:59:18  matt
  57.  * Do error checking on list of angles
  58.  * 
  59.  * Revision 1.11  1994/06/09  16:21:32  matt
  60.  * Took out special-case and test code.
  61.  * 
  62.  * Revision 1.10  1994/06/07  13:21:14  matt
  63.  * Added support for new chunk-based POF files, with robot animation data.
  64.  * 
  65.  * Revision 1.9  1994/06/01  17:58:24  mike
  66.  * Greater flinch effect.
  67.  * 
  68.  * Revision 1.8  1994/06/01  14:59:25  matt
  69.  * Fixed calc_gun_position(), which was rotating the wrong way for the
  70.  * object orientation.
  71.  * 
  72.  * Revision 1.7  1994/06/01  12:44:04  matt
  73.  * Added flinch state for test robot
  74.  * 
  75.  * Revision 1.6  1994/05/31  19:17:24  matt
  76.  * Fixed test robot angles
  77.  * 
  78.  * Revision 1.5  1994/05/30  19:43:50  mike
  79.  * Call set_test_robot.
  80.  * 
  81.  * 
  82.  * Revision 1.4  1994/05/30  00:02:44  matt
  83.  * Got rid of robot render type, and generally cleaned up polygon model
  84.  * render objects.
  85.  * 
  86.  * Revision 1.3  1994/05/29  18:46:15  matt
  87.  * Added stuff for getting robot animation info for different states
  88.  * 
  89.  * Revision 1.2  1994/05/26  21:09:15  matt
  90.  * Moved robot stuff out of polygon model and into robot_info struct
  91.  * Made new file, robot.c, to deal with robots
  92.  * 
  93.  * Revision 1.1  1994/05/26  18:02:04  matt
  94.  * Initial revision
  95.  * 
  96.  * 
  97.  */
  98.  
  99.  
  100. #pragma off (unreferenced)
  101. static char rcsid[] = "$Id: robot.c,v 1.1.1.1 1998/03/03 15:12:30 nobody Exp $";
  102. #pragma on (unreferenced)
  103.  
  104. #include "error.h"
  105.  
  106. #include "inferno.h"
  107.  
  108. #include "robot.h"
  109. #include "object.h"
  110. #include "polyobj.h"
  111. #include "mono.h"
  112.  
  113. int    N_robot_types = 0;
  114. int    N_robot_joints = 0;
  115.  
  116. //    Robot stuff
  117. robot_info Robot_info[MAX_ROBOT_TYPES];
  118.  
  119. //Big array of joint positions.  All robots index into this array
  120.  
  121. #define deg(a) ((int) (a) * 32768 / 180)
  122.  
  123. //test data for one robot
  124. jointpos Robot_joints[MAX_ROBOT_JOINTS] = {
  125.  
  126. //gun 0
  127.                     {2,{deg(-30),0,0}},        //rest (2 joints)
  128.                     {3,{deg(-40),0,0}},
  129.  
  130.                     {2,{deg(0),0,0}},            //alert
  131.                     {3,{deg(0),0,0}},
  132.         
  133.                     {2,{deg(0),0,0}},            //fire
  134.                     {3,{deg(0),0,0}},
  135.         
  136.                     {2,{deg(50),0,0}},        //recoil
  137.                     {3,{deg(-50),0,0}},
  138.         
  139.                     {2,{deg(10),0,deg(70)}},        //flinch
  140.                     {3,{deg(0),deg(20),0}},
  141.         
  142. //gun 1
  143.                     {4,{deg(-30),0,0}},        //rest (2 joints)
  144.                     {5,{deg(-40),0,0}},
  145.  
  146.                     {4,{deg(0),0,0}},            //alert
  147.                     {5,{deg(0),0,0}},
  148.         
  149.                     {4,{deg(0),0,0}},            //fire
  150.                     {5,{deg(0),0,0}},
  151.         
  152.                     {4,{deg(50),0,0}},        //recoil
  153.                     {5,{deg(-50),0,0}},
  154.         
  155.                     {4,{deg(20),0,deg(-50)}},    //flinch
  156.                     {5,{deg(0),0,deg(20)}},
  157.         
  158. //rest of body (the head)
  159.  
  160.                     {1,{deg(70),0,0}},        //rest (1 joint, head)
  161.  
  162.                     {1,{deg(0),0,0}},            //alert
  163.         
  164.                     {1,{deg(0),0,0}},            //fire
  165.         
  166.                     {1,{deg(0),0,0}},            //recoil
  167.  
  168.                     {1,{deg(-20),deg(15),0}},            //flinch
  169.  
  170.  
  171. };
  172.  
  173. //given an object and a gun number, return position in 3-space of gun
  174. //fills in gun_point
  175. void calc_gun_point(vms_vector *gun_point,object *obj,int gun_num)
  176. {
  177.     polymodel *pm;
  178.     robot_info *r;
  179.     vms_vector pnt;
  180.     vms_matrix m;
  181.     int mn;                //submodel number
  182.  
  183.     Assert(obj->render_type==RT_POLYOBJ || obj->render_type==RT_MORPH);
  184.     Assert(obj->id < N_robot_types);
  185.  
  186.     r = &Robot_info[obj->id];
  187.     pm =&Polygon_models[r->model_num];
  188.  
  189.     if (gun_num >= r->n_guns)
  190.     {
  191.         mprintf((1, "Bashing gun num %d to 0.\n", gun_num));
  192.         Int3();
  193.         gun_num = 0;
  194.     }
  195.  
  196. //    Assert(gun_num < r->n_guns);
  197.  
  198.     pnt = r->gun_points[gun_num];
  199.     mn = r->gun_submodels[gun_num];
  200.  
  201.     //instance up the tree for this gun
  202.     while (mn != 0) {
  203.         vms_vector tpnt;
  204.  
  205.         vm_angles_2_matrix(&m,&obj->rtype.pobj_info.anim_angles[mn]);
  206.         vm_transpose_matrix(&m);
  207.         vm_vec_rotate(&tpnt,&pnt,&m);
  208.  
  209.         vm_vec_add(&pnt,&tpnt,&pm->submodel_offsets[mn]);
  210.  
  211.         mn = pm->submodel_parents[mn];
  212.     }
  213.  
  214.     //now instance for the entire object
  215.  
  216.     vm_copy_transpose_matrix(&m,&obj->orient);
  217.     vm_vec_rotate(gun_point,&pnt,&m);
  218.     vm_vec_add2(gun_point,&obj->pos);
  219.     
  220. }
  221.  
  222. //fills in ptr to list of joints, and returns the number of joints in list
  223. //takes the robot type (object id), gun number, and desired state
  224. int robot_get_anim_state(jointpos **jp_list_ptr,int robot_type,int gun_num,int state)
  225. {
  226.  
  227.     Assert(gun_num <= Robot_info[robot_type].n_guns);
  228.  
  229.     *jp_list_ptr = &Robot_joints[Robot_info[robot_type].anim_states[gun_num][state].offset];
  230.  
  231.     return Robot_info[robot_type].anim_states[gun_num][state].n_joints;
  232.  
  233. }
  234.  
  235.  
  236. //for test, set a robot to a specific state
  237. set_robot_state(object *obj,int state)
  238. {
  239.     int g,j,jo;
  240.     robot_info *ri;
  241.     jointlist *jl;
  242.  
  243.     Assert(obj->type == OBJ_ROBOT);
  244.  
  245.     ri = &Robot_info[obj->id];
  246.  
  247.     for (g=0;g<ri->n_guns+1;g++) {
  248.  
  249.         jl = &ri->anim_states[g][state];
  250.  
  251.         jo = jl->offset;
  252.  
  253.         for (j=0;j<jl->n_joints;j++,jo++) {
  254.             int jn;
  255.  
  256.             jn = Robot_joints[jo].jointnum;
  257.  
  258.             obj->rtype.pobj_info.anim_angles[jn] = Robot_joints[jo].angles;
  259.  
  260.         }
  261.     }
  262. }
  263.  
  264. #include "mono.h"
  265.  
  266. //--unused-- int cur_state=0;
  267.  
  268. //--unused-- test_anim_states()
  269. //--unused-- {
  270. //--unused--     set_robot_state(&Objects[1],cur_state);
  271. //--unused-- 
  272. //--unused--     mprintf(0,"Robot in state %d\n",cur_state);
  273. //--unused-- 
  274. //--unused--     cur_state = (cur_state+1)%N_ANIM_STATES;
  275. //--unused-- 
  276. //--unused-- }
  277.  
  278. //set the animation angles for this robot.  Gun fields of robot info must
  279. //be filled in.
  280. robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS])
  281. {
  282.     int m,g,state;
  283.     int gun_nums[MAX_SUBMODELS];            //which gun each submodel is part of
  284.  
  285.     for (m=0;m<pm->n_models;m++)
  286.         gun_nums[m] = r->n_guns;        //assume part of body...
  287.  
  288.     gun_nums[0] = -1;        //body never animates, at least for now
  289.  
  290.     for (g=0;g<r->n_guns;g++) {
  291.         m = r->gun_submodels[g];
  292.  
  293.         while (m != 0) {
  294.             gun_nums[m] = g;                //...unless we find it in a gun
  295.             m = pm->submodel_parents[m];
  296.         }
  297.     }
  298.  
  299.     for (g=0;g<r->n_guns+1;g++) {
  300.  
  301.         //mprintf(0,"Gun %d:\n",g);
  302.  
  303.         for (state=0;state<N_ANIM_STATES;state++) {
  304.  
  305.             //mprintf(0," State %d:\n",state);
  306.  
  307.             r->anim_states[g][state].n_joints = 0;
  308.             r->anim_states[g][state].offset = N_robot_joints;
  309.  
  310.             for (m=0;m<pm->n_models;m++) {
  311.                 if (gun_nums[m] == g) {
  312.                     //mprintf(0,"  Joint %d: %x %x %x\n",m,angs[state][m].pitch,angs[state][m].bank,angs[state][m].head);
  313.                     Robot_joints[N_robot_joints].jointnum = m;
  314.                     Robot_joints[N_robot_joints].angles = angs[state][m];
  315.                     r->anim_states[g][state].n_joints++;
  316.                     N_robot_joints++;
  317.                     Assert(N_robot_joints < MAX_ROBOT_JOINTS);
  318.                 }
  319.             }
  320.         }
  321.     }
  322.  
  323. }
  324.  
  325.  
  326.